import axios from 'axios'

/**
 * 请求拦截器
 */
axios.interceptors.request.use(
  function (config) {
    const token = localStorage.getItem('token')
    // console.log(token)
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  function (error) {
    const { statu } = error.response
    if (statu === 401) {
      window.location.href = '#/login'
    }
    return Promise.reject(error)
  }
)

/**
 * 响应拦截器
 */
axios.interceptors.response.use(
  function (response) {
    const { token } = response.data
    token && localStorage.setItem('token', token)
    return response
  },
  function (error) {
    return Promise.reject(error)
  }
)
